from abc import ABC

from apps.delete.form import DeleteProductForm
from apps.api.delete_offer import DeleteOfferApi
from Alibb.handlers import BaseHandler
from tools.auth import authenticated_async

from ujson import loads


class DeleteOfferHandler(BaseHandler, ABC):

    @authenticated_async
    async def get(self):

        body = loads(self.request.body.decode('utf8'))

        form = DeleteProductForm.from_json(body)

        if not form.validate():
            for error in form.errors:
                self.set_status(403)
                return await self.finish({"error": form.errors[error][0]})

        api = DeleteOfferApi(self.current_user['access_token'],
                             self.application.session,
                             self.application.settings['app_key'])

        param = {
            "productID": form.product_id.data,
            "webSite": form.webSite.data
        }

        status_code, resp = await api.post(param)

        await self.finish(resp)
